//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    c_name: '',
    active: 6,
    html: ''
  },

  onLoad: function () {
    wx.showLoading({
      title: '加载中...',
    })
  },

  onShow() {
    this.loadWork()
  },

  toDetail(e) {
    console.log(e)
    let solve_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../detail/detail?id=' + solve_id,
    })
  },

  // 选项卡  主要是通过判断e.target.id的值 设置对应的id显示
  switchNav(e) {
    wx.showLoading({
      title: '加载中...',
    })
    let id = e.currentTarget.id;
    this.setData({
      active: id
    })
    setTimeout(() => {
      wx.hideLoading()
      this.loadCom()
      this.loadWork()
      this.loadDangFei()
      this.loadDiBao()
      this.loadBuTie()
      this.loadZhengJian()
      this.loadCanJun()
    }, 500)
  },

  loadCom() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/solvelist',
      data: {
        access_token: access_token,
      },
      success: (res) => {
        if (res.data.code >= 500) {
          app.getLogin().then(function () {
            console.log('二次登录。。。')
          })
        }
        else if (res.data.code == 1) {
          this.setData({
            comList: res.data.list
          })
          wx.setStorageSync('comList', res.data.list)
        }
      },
      complete: (res) => {
        wx.hideLoading()
      }
    })
  },
  
  // 组织关系转接
  loadWork() {
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/work',
      data: {
        access_token: access_token,
        type: 'work_relation'
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            relation:res.data.info
          })
        }
      },
      complete: (res) => {
        wx.hideLoading()
      }
    })
  },

  // 缴纳党费
  loadDangFei(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/work',
      data: {
        access_token: access_token,
        type: 'work_df'
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            dangfei:res.data.info
          })
        }
      },
    })
  },

  // 申请低保
  loadDiBao(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/work',
      data: {
        access_token: access_token,
        type: 'work_db'
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            dibao:res.data.info
          })
        }
      },
    })
  },

  // 申请补贴
  loadBuTie(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/work',
      data: {
        access_token: access_token,
        type: 'work_bt'
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            butie:res.data.info
          })
        }
      },
    })
  },
  
  // 办理证件
  loadZhengJian(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/work',
      data: {
        access_token: access_token,
        type: 'work_zj'
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            zhengjian:res.data.info
          })
        }
      },
    })
  },
  
  // 我要参军
  loadCanJun(){
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/work',
      data: {
        access_token: access_token,
        type: 'work_cj'
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          this.setData({
            canjun:res.data.info
          })
        }
      },
    })
  },

  inputBind(e) {
    if (e.detail.value == '') {
      this.loadCom()
    }
  },

  query(e) {
    this.setData({
      c_name: e.detail.value
    })
    let c_name = this.data.c_name;
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/solvelist',
      data: {
        access_token: access_token,
        c_name: c_name
      },
      success: (res) => {
        if (res.data.code == 1) {
          this.setData({
            comList: res.data.list
          })
        }
      }
    })
  }
})
